using System.Collections.Generic;
using AdvancedInspector;
using UnityEngine;

[AdvancedInspector(true)]
public class CameraModeControl : MonoBehaviour
{
	public static CameraModeControl instance;

	private GameObject cam;

	public settings.MainCameraType mainCameraType;

	private bool lookAt;

	private void Awake()
	{
		mainCameraType = (settings.MainCameraType)Load.LoadInt(settings.keyCamActive);
		cam = GameController.instance.cameraGame;
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
		if (Load.LoadInt(settings.keyCamActive) != Load.LoadInt("OldCam") && Load.LoadBool("NormalSession"))
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Selected_Camera_Mode", ((settings.MainCameraType)Load.LoadInt(settings.keyCamActive)).ToString());
			Save.SaveInt("OldCam", Load.LoadInt(settings.keyCamActive));
			FlurryWrapper.LogEvent(FlurryWrapper.EV_CAMERA_TYPE, dictionary);
		}
	}

	private void Start()
	{
		EnablePlayerSelectedCamera(false, true);
	}

	public void EnableTpsCamera(bool smoothTargetChange, bool resetPosition)
	{
		if (!GameController.instance.playerScript)
		{
			return;
		}
		GameController.instance.playerScript.HideOpticalAimer();
		mainCameraType = settings.MainCameraType.TPSCamera;
		if (!GameController.instance.playerScript.inCar && !GameController.instance.playerScript.inHelic)
		{
			FPSCamera.instance.Disable();
			TPSCamera.instance.enabled = true;
			TPSCamera.instance.SetCamera(TPSCamera.TPSCameraType.Side, null, smoothTargetChange, resetPosition, true);
			if (NGUITools.FindCameraForLayer(28) != null)
			{
				NGUITools.FindCameraForLayer(28).fieldOfView = 60f;
			}
		}
		GameController.instance.playerScript.RefreshWaterState();
		GameController.instance.playerScript.RefreshWeaponAnimationReloadSpeed();
	}

	public void EnableRPGCamera(bool smoothTargetChange, bool resetPosition)
	{
		GameController.instance.playerScript.HideOpticalAimer();
		mainCameraType = settings.MainCameraType.RPGCamera;
		if (!GameController.instance.playerScript.inCar && !GameController.instance.playerScript.inHelic)
		{
			FPSCamera.instance.Disable();
			TPSCamera.instance.enabled = true;
			TPSCamera.instance.SetCamera(TPSCamera.TPSCameraType.Side, null, smoothTargetChange, resetPosition, true);
			if (NGUITools.FindCameraForLayer(28) != null)
			{
				NGUITools.FindCameraForLayer(28).fieldOfView = 60f;
			}
		}
		GameController.instance.playerScript.RefreshWaterState();
		GameController.instance.playerScript.RefreshWeaponAnimationReloadSpeed();
	}

	public void EnableFPSCamera()
	{
		if (!settings.offlineMode && GameController.instance.curTypeGame == TypeGame.Zombe && TDMController.myTeam == PunTeams.Team.red)
		{
			EnableTpsCamera(false, true);
		}
		else
		{
			if (!(GameController.instance.playerScript != null))
			{
				return;
			}
			GameController.instance.playerScript.HideOpticalAimer();
			mainCameraType = settings.MainCameraType.FPSCamera;
			if (!GameController.instance.playerScript.inCar && !GameController.instance.playerScript.inHelic)
			{
				TPSCamera.instance.enabled = false;
				FPSCamera.instance.Enable();
				FPSCamera.instance.Reset();
				if (NGUITools.FindCameraForLayer(28) != null)
				{
					NGUITools.FindCameraForLayer(28).fieldOfView = 70f;
				}
			}
			GameController.instance.playerScript.RefreshWaterState();
			GameController.instance.playerScript.RefreshWeaponAnimationReloadSpeed();
		}
	}

	private void EnablePlayerSelectedCamera(bool smoothTargetChange, bool resetPosition)
	{
		settings.MainCameraType mainCameraType = (settings.MainCameraType)Load.LoadInt(settings.keyCamActive);
		if (!settings.offlineMode && GameController.instance.curTypeGame == TypeGame.Zombe && mainCameraType == settings.MainCameraType.FPSCamera && TDMController.myTeam == PunTeams.Team.red)
		{
			mainCameraType = settings.MainCameraType.TPSCamera;
		}
		switch (mainCameraType)
		{
		case settings.MainCameraType.TPSCamera:
			EnableTpsCamera(smoothTargetChange, resetPosition);
			break;
		case settings.MainCameraType.RPGCamera:
			EnableRPGCamera(smoothTargetChange, resetPosition);
			break;
		case settings.MainCameraType.FPSCamera:
			EnableFPSCamera();
			break;
		}
	}

	public void OnDeath()
	{
		lookAt = true;
		TPSCamera.instance.enabled = false;
	}

	public void OnRespawn()
	{
		lookAt = false;
		TPSCamera.instance.isRotating = false;
		TPSCamera.instance.dragCamera = false;
		CameraZoneEnabler.GetCameraZone().pointerDown = false;
		EnablePlayerSelectedCamera(false, true);
		GameTopWindow.instance.deathMessagePanel.gameObject.SetActive(false);
	}

	private void Update()
	{
		if (lookAt && GameController.instance.myPlayer != null)
		{
			cam.transform.LookAt(GameController.instance.myPlayer.transform.position + Vector3.up);
		}
	}

	public void OnEnterCar(Transform car)
	{
		if (mainCameraType == settings.MainCameraType.FPSCamera)
		{
			GameController.instance.playerScript.mecanimWrapper.inWater = false;
			FPSCamera.instance.Disable();
			TPSCamera.instance.enabled = true;
			TPSCamera.instance.SetCamera(TPSCamera.TPSCameraType.Vehicle, car, false, true);
		}
		else
		{
			TPSCamera.instance.SetCamera(TPSCamera.TPSCameraType.Vehicle, car, GameController.instance.curTypeGame != TypeGame.tanks);
		}
	}

	public void ShakeCameraExplosion(Transform explosionTransform, float maxDistance)
	{
		switch (mainCameraType)
		{
		case settings.MainCameraType.TPSCamera:
			TPSCamera.instance.ShakeCameraExplosion(explosionTransform, maxDistance);
			break;
		case settings.MainCameraType.FPSCamera:
			FPSCamera.instance.ShakeCameraExplosion(explosionTransform, maxDistance);
			break;
		case settings.MainCameraType.RPGCamera:
			break;
		}
	}

	public void OutFromCar()
	{
		EnablePlayerSelectedCamera(true, false);
	}

	private void DisableSelectedCamera()
	{
		switch (mainCameraType)
		{
		case settings.MainCameraType.TPSCamera:
			TPSCamera.instance.enabled = false;
			break;
		case settings.MainCameraType.RPGCamera:
			TPSCamera.instance.enabled = false;
			break;
		case settings.MainCameraType.FPSCamera:
			FPSCamera.instance.Disable();
			break;
		}
	}
}
